package com.iwhalecloud.bss.kite.cucc.manager.scene.service.operate;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.common.validate.annotation.KiteNotNull;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.dataservice.util.IdUtils;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.dto.ContAttachmentDTO;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.enums.RelType;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInstContract;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.manager.inst.graph.impl.InstVertex;
import com.iwhalecloud.bss.kite.manager.scene.service.operate.OperateInstService;
import com.iwhalecloud.bss.kite.manager.scene.util.InstRelGraphUtils;
import com.iwhalecloud.bss.kite.manager.scene.util.SceneInstUtils;
import com.iwhalecloud.bss.kite.manager.transaction.TransactionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 停机记录实例处理类
 */
@Service("manager.localOperOfferInstContractRelService")
public class LocalOperateOfferInstContractRelService extends OperateInstService {

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;
    /**
     * 添加接入产品实例停机记录
     * @param sceneInstId
     * @param prodInstId
     * @param stopType
     * @return
     */
    @Validated
    public KiteOfferInstContract addOfferInstContractRel(String sceneInstId, String offerInstInstId, InstType offerInstType, String contractBatchId, String contractNo, String contractName, String contractUrl,String contractType,List<ContAttachmentDTO> attachments) {
        // 只能选一份合同
        KiteOfferInstContract offerInstContractRel = new KiteOfferInstContract();
        offerInstContractRel.setInstId(IdUtils.generateInstId(InstType.PROD_INST_ACCT));
        offerInstContractRel.setOfferInstId(offerInstInstId);
        //合同批次号
        offerInstContractRel.setContractBatchId(contractBatchId);
        //合同编号
        offerInstContractRel.setContractNo(contractNo);
        offerInstContractRel.setContractName(contractName);
        offerInstContractRel.setContractUrl(contractUrl);
        offerInstContractRel.setInstType(InstType.OFFER_INST_CONTRACT);
        offerInstContractRel.setActionType(ActionType.A);
        /*if(KiteStringUtils.equals(contractType,"3")) {
            offerInstContractRel.setContractType("01");
        }else {
            offerInstContractRel.setContractType("02");
        }*/
        offerInstContractRel.setAttachments(attachments);
        offerInstContractRel.setContractType(contractType);
        String custId = querySceneInstService.getSceneInst(sceneInstId).getCustId();

        KiteOfferInst offerInst = querySceneInstService.getOfferInst(custId, offerInstInstId);
        if (KiteObjectUtils.isEmpty(offerInst)) {
            ErrorConsts.OFFER_INST_NOT_EXIST.throwOut(offerInstInstId, custId);
        }
        // 没有账户明细关系，明细ID传销售品id
        String offerId = Optional.ofNullable(OfferCache.get(offerInst.getOfferId())).map(Offer::getOfferId).orElse("");
        String offerObjInstRelId = operateSceneInstService.addOfferInstContract(sceneInstId, offerInstContractRel, offerInstInstId, RelType.OFFER_OBJ_REL_TYPE_190000, ActionType.A, KeyConsts.OFFER_CONTRACT_ROLE, offerId);
        offerInstContractRel.setOfferObjInstRelId(offerObjInstRelId);
        //kiteOfferInstContractRelList.add(offerInstContractRel);

        //operateSceneInstService.putExtParameter(sceneInstId, "offerInstContractRel", kiteOfferInstContractRelList);

        return offerInstContractRel;
    }


    public List<KiteOfferInstContract> listOfferInstInstContractRels(String sceneInstId, String custId, String offerInstId) {
        List<KiteOfferInstContract> contractRels = querySceneInstService.listOfferInstContracts(custId, offerInstId);
        if (contractRels == null) {
            contractRels = new ArrayList<>();
            return contractRels;
        }
        if (!KiteStringUtils.isEmpty(offerInstId)) {
            contractRels = contractRels.stream().filter((contract) -> contract.getOfferInstId().equals(offerInstId)).collect(Collectors.toList());
        }
        return contractRels;
    }

    /**
     * 添加接入产品实例停机记录
     * @param sceneInstId
     * @param prodInstId
     * @param prodInstState
     * @param relActionType
     * @return
     */
    @Validated
    public String addOfferInstContractRel(String sceneInstId, String offerInstId, InstType offerInstType, @KiteNotNull KiteOfferInstContract offerInstContractRel, RelType relType, ActionType relActionType) {
        relActionType = Optional.ofNullable(relActionType).orElse(ActionType.A);

        KiteSceneInst sceneInst = TransactionContext.getContainer().getSceneInst(sceneInstId);
        String custId = SceneInstUtils.getCustId(sceneInst);
        InstVertex instVertex = InstRelGraphUtils.getCustInstVertex(custId, offerInstType, offerInstId);
        if (Objects.isNull(instVertex)) {
            ErrorConsts.OFFER_INST_NOT_EXIST.throwOut(offerInstId, custId);
        }


        return addSubInst(sceneInst, offerInstType, offerInstId, offerInstContractRel, relType, relActionType);
    }

    /**
     * 删除接入产品实例停机记录
     * @param sceneInstId
     * @param prodInstId
     * @param stopType
     * @return
     */
    public Boolean deleteOfferInstContractRel(String sceneInstId, String offerInstId, InstType offerInstType, String offerInstContractRelId) {
        /*KiteSceneInst sceneInst = TransactionContext.getContainer().getSceneInst(sceneInstId);
        String custId = SceneInstUtils.getCustId(sceneInst);
        IInstRelGraph custInstRelGraph = TransactionContext.getContainer().getCustInstRelGraph(custId);
        InstVertex instVertex = InstRelGraphUtils.getCustInstVertex(custId, InstType.OFFER_INST_CONTRACT_REL, offerInstContractRelId);
        if (Objects.isNull(instVertex)) {
            ErrorConsts.OFFER_INST_NOT_EXIST.throwOut(offerInstContractRelId, custId);
        }

        InstType parentInstType = null;
        String parentInstId = "";
        List<InstVertex> prodInstVertexs = InstRelGraphUtils.listParentInstVertexs(custInstRelGraph, instVertex,
            RelType.OFFER_CONTRACT_REL_TYPE_1000, offerInstType);
        InstVertex prodInstVertex = InstRelGraphUtils.getInstVertex(prodInstVertexs);
        if (Objects.nonNull(prodInstVertex)) {
            parentInstType = prodInstVertex.getInstType();
            parentInstId = prodInstVertex.getInstId();
        }
        return deleteSubInst(sceneInst, parentInstType, parentInstId, InstType.OFFER_INST_CONTRACT_REL, offerInstContractRelId);*/

        operateSceneInstService.deleteOfferInstContract(sceneInstId, offerInstContractRelId);

        //operateSceneInstService.removeExtParameter(sceneInstId, "offerInstContractRel");
        return Boolean.TRUE;

    }
}
